<script setup>
	import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'

	import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
	import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
	import {
		ref
	} from 'vue'
	import dayjs from 'dayjs'
	import optionDeptPerson from '@/store/modules/optionDeptPerson'
	import {
		addAdministrativeApply,
		addFinancialApplyControllerApi
	} from '@/api/attendance'
	import {
		getUserProfile
	} from '@/api/system/user'
	import approvalProcessState from '@/store/modules/approvalProcess'
	import {
		isObjectValuesNotEmpty
	} from '@/utils/objIsnull.js'
	// 弃用
	const title = '物品领用申请'

	const entertainDateRef = ref(null)
	const ItemRequisitionList = ref({
		date: '',
		received: '',//领用用途
		classification: '',//领用用途分类
		itemRequisitionDetail: [{
			goodsName: '',
			goodsNumber: '',
			goodsUnit: '',
			goodsPurpose: '',
		}, ],
	})
	const userId = ref(null)
	const deptId = ref(null)
	let attchment = ref([])
	const getDesignDrawingsEmit = (emit) => {
		attchment.value = emit
	}
	const clickSave = async () => {
		const RequiredItemList = JSON.parse(JSON.stringify(ItemRequisitionList.value))
		delete RequiredItemList.received
		console.log(RequiredItemList,'RequiredItemList')
		if (isObjectValuesNotEmpty(RequiredItemList)) {
			let params = {
				date: dayjs(ItemRequisitionList.value.date).format('YYYY-MM-DD hh:mm:ss'),
				details: JSON.stringify(ItemRequisitionList.value.itemRequisitionDetail),
				received: ItemRequisitionList.value.received,
				classification: ItemRequisitionList.value.classification,
				ruleId: 'wupin',
				type: '物品领用申请',
				attchment: JSON.stringify(attchment.value),
				userId: userId.value,
				deptId:deptId.value,
				approve: approvalProcessState.state.approvalProcess
			}
			let {
				code
			} = await addAdministrativeApply(params)
			if (code === 200) {
				uni.showToast({
					title: '提交成功',
					duration: 1500,
					icon: 'success',
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			}
		} else {
			uni.showToast({
				title: '请输入完整数据',
				icon: 'error'
			})
		}
	}
	const goHistoryView = () => {
		uni.navigateTo({
			url: '/components/TypeDefineModel?type=物品领用申请&apiName=行政'
		})
	}
	const purposeTypeRef = ref(null)
	const purposeTypeColumns = [
		['自用', '招待维系']
	]
	const purposeTypeConfirm = (e) => {
		ItemRequisitionList.value.classification = e.value[0]
	}
	const purposeTypeFn = () => {
		purposeTypeRef.value.open()
	}

	const formatter = (type, value) => {
		if (type === 'year') {
			return `${value}年`
		}
		if (type === 'month') {
			return `${value}月`
		}
		if (type === 'day') {
			return `${value}日`
		}
		if (type === 'hour') {
			return `${value}时`
		}
		if (type === 'minute') {
			return `${value}分`
		}
		return value
	}

	const entertainDateFn = () => {
		entertainDateRef.value.open()
	}
	let entertainDatePicker = dayjs()
	const entertainDateConfirm = (obj) => {
		ItemRequisitionList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
	}


	const copyDetailItem = () => {
		ItemRequisitionList.value.itemRequisitionDetail.push({
			goodsName: '',
			goodsNumber: '',
			goodsUnit: '',
			goodsPurpose: '',
		})
	}
	const deleteDetailItem = (index) => {
		ItemRequisitionList.value.itemRequisitionDetail.splice(index, 1)
	}
	const getUserProfileFn = async () => {
		let {
			code,
			data
		} = await getUserProfile()
		if (code === 200) {
			userId.value = data.userId	
			deptId.value = data.deptId
		}
	}

	onLoad(() => {
		getUserProfileFn()
	})
</script>

<template>
	<view>
		<!--    请假  -->
		<HeaderNavTabarSvg :title="title">
			<image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
		</HeaderNavTabarSvg>

		<view class="picker-content" @tap="entertainDateFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">日期</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="ItemRequisitionList.date">{{ ItemRequisitionList.date }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="entertainDateRef" v-model="entertainDatePicker" :formatter="formatter" mode="date"
			@confirm="entertainDateConfirm" />

		<view class="picker-content" @tap="purposeTypeFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">领用用途分类</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="ItemRequisitionList.classification">{{ ItemRequisitionList.classification }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-picker ref="purposeTypeRef" :columns="purposeTypeColumns" @confirm="purposeTypeConfirm"></uv-picker>

		<view class="textarea-box">
			<text class="info-list-left">领用用途</text>
			<textarea v-model="ItemRequisitionList.received" class="textinfo-list" placeholder="请输入"></textarea>
		</view>

		<view class="detail-itemRequisition" v-for="(item, index) in ItemRequisitionList.itemRequisitionDetail"
			:key="index">
			<view class="detail-item">
				<p style="color: #86909C">明细{{index+1}}</p>
				<p class="detail-utils">
					<text @tap="copyDetailItem">复制</text>
					<text @tap="deleteDetailItem(index)" v-if="ItemRequisitionList.itemRequisitionDetail.length > 1">删除</text>
				</p>
			</view>

			<view class="picker-content">
				<view class="measurement-info-list">
					<text class="info-list-left isrequired">物品名称</text>
					<view class="info-list-right">
						<view class="info-content">
							<input adjust-position type="text" class="uni-input" placeholder="请输入" v-model="item.goodsName" />
						</view>
					</view>
				</view>
			</view>
			<view class="picker-content">
				<view class="measurement-info-list">
					<text class="info-list-left isrequired">物品数量</text>
					<view class="info-list-right">
						<view class="info-content">
							<input adjust-position type="text" class="uni-input" placeholder="请输入" v-model="item.goodsNumber" />
						</view>
					</view>
				</view>
			</view>
			<view class="picker-content">
				<view class="measurement-info-list">
					<text class="info-list-left isrequired">物品单位</text>
					<view class="info-list-right">
						<view class="info-content">
							<input adjust-position type="text" class="uni-input" placeholder="请输入" v-model="item.goodsUnit" />
						</view>
					</view>
				</view>
			</view>
			<view class="textarea-box">
				<text class="info-list-left isrequired">物品用途</text>
				<textarea v-model="item.goodsPurpose" class="textinfo-list" placeholder="请输入"></textarea>
			</view>

		</view>

		<!--    说明附件-->
		<DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" />
		<!--    审批流程-->
		<AuditApplicationSteps :ruleId="'wupin'" :purpose="ItemRequisitionList.classification" />
		<!--    底部占位符-->
		<view class="blank"></view>
		<!--    保存-->
		<view class="buttonContainer">
			<button class="buttonClass" @click="clickSave">提交</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	@import '@/static/style/index.scss';

	.bukaSvg {
		width: 32rpx;
		height: 32rpx;
	}

	.buttonContainer {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		width: 100%;
		align-items: center;
		padding: 16rpx;
		background-color: #fff;
	}

	.buttonClass {
		display: flex;
		width: 686rpx;
		height: 72rpx;
		justify-content: center;
		align-items: center;
		background: #165dff;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
		//margin-top: 40rpx;
		overflow: hidden;
	}

	.uni-inputs {
		color: #86909c !important;
	}

	.uni-input {
		text-align: right;
		font-size: 28rpx;
	}

	.blank {
		height: calc(72rpx + 32rpx + 20rpx);
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.bukabanci {
		.info-content {
			color: #86909c !important;
		}

		.info-list-left {
			// @include font-color-auxiliary;
			color: #86909c !important;
		}
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.popup-content {
		background: #f7f8fa;

		.header-box {
			padding: 30rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			p {
				color: #1d2129;
				font-size: 34rpx;
				font-weight: 500;
			}

			.module-image-ba {
				border-radius: 32rpx;
				background: #f2f3f5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}

		.card-list {
			display: flex;
			align-items: center;
			flex-direction: column;
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.list {
				box-shadow: 0 4rpx 16rpx 0 #0000000d;
				box-sizing: border-box;
				width: 100%;
				background: #fff;
				padding: 16rpx 24rpx;
				border-radius: 8rpx;
				margin-bottom: 10rpx;

				h4 {
					color: #1d2129;
					font-size: 32rpx;
					font-weight: 400;
				}

				p {
					color: #86909c;
					font-size: 24rpx;
					font-weight: 400;
					margin-top: 10rpx;
				}
			}
		}
	}

	.detail-itemRequisition {
		.detail-item {
			padding: 10rpx 30rpx;
			@include flex($align: center, $justify: space-between);

			.detail-utils {
				@include flex($align: center, $justify: space-between);
				width: 150rpx;
				color: #165DFF;
			}


		}

		.text-info {
			color: #86909C;
			padding: 20rpx 30rpx;
			display: inline-block;
		}
	}


	.measurement-info-list {
		@include flex($align: center, $justify: space-between);
		padding: $spacing-row-base;
		background: #fff;
		margin-top: 16rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.info-list-right {
			@include flex($align: center);

			.info-content {
				@include textStyleBase;
				font-size: 34rpx;
			}

			.iconfont {
				margin-left: 16rpx;
				// @include font-color-auxiliary;
				color: $uni-fc-fu-lin;
			}
		}

		.unit {
			// @include font-color-auxiliary;
			color: $uni-fc-fu-lin;
		}
	}
</style>